package com.getpebble.android.common.model;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblContentProviderUtil;
import com.getpebble.android.common.model.PblDataModel.Column;
import com.getpebble.android.common.model.PblDataModel.Column.Type;
import com.getpebble.android.util.SQLUtil;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;

public class LanguagePackModel extends PblDataModel {
    private static final Uri CUSTOM_RAW_QUERY_URI = PblContentProviderUtil.getTableUri("language_pack_custom_select_uri");
    public static final Uri TABLE_URI = PblContentProviderUtil.getTableUri("pebble_language_packs");
    private static final String TAG = LanguagePackModel.class.getSimpleName();
    private static final String[] UNIQUE_COLUMNS = new String[]{"iso_locale", "hw_platform", "fw_version", "language_version"};

    public LanguagePackModel() throws IllegalArgumentException {
        super("pebble_language_packs");
        addColumn(new Column(Type.STRING, "file_url"));
        addColumn(new Column(Type.STRING, "language_name"));
        addColumn(new Column(Type.STRING, "local_language_name"));
        addColumn(new Column(Type.STRING, "iso_locale"));
        addColumn(new Column(Type.STRING, "hw_platform"));
        addColumn(new Column(Type.STRING, "fw_version"));
        addColumn(new Column(Type.STRING, "language_version"));
        addColumn(new Column(Type.STRING, "language_uid"));
        addColumn(new Column(Type.INTEGER, "numeric_fw_version"));
    }

    public int getNumColumns() {
        return getColumnSet().size();
    }

    public String getCreateTableCommand() {
        return SQLUtil.getCreateTableCommandWithUniqueConstraint(super.getCreateTableCommand(), ImmutableList.of(Arrays.asList(getUniqueColumns())));
    }

    public static String[] getUniqueColumns() {
        return UNIQUE_COLUMNS;
    }

    public static int getLanguageCount(ContentResolver contentResolver, FirmwareVersion firmwareVersion, HardwarePlatform hardwarePlatform) {
        if (firmwareVersion == null || hardwarePlatform == null) {
            return 0;
        }
        Cursor cursor = query(contentResolver, firmwareVersion, hardwarePlatform);
        if (cursor.moveToFirst()) {
            int count = cursor.getCount();
            cursor.close();
            return count;
        }
        cursor.close();
        return 0;
    }

    public static int getNumericFwVersion(FirmwareVersion firmwareVersion) {
        return ((firmwareVersion.getMajor() * 10000) + (firmwareVersion.getMinor() * 100)) + firmwareVersion.getPoint();
    }

    public static Cursor query(ContentResolver contentResolver, FirmwareVersion firmwareVersion, HardwarePlatform hardwarePlatform) throws IllegalArgumentException {
        if (firmwareVersion == null) {
            throw new IllegalArgumentException("Firmware version cannot be null");
        } else if (hardwarePlatform == null) {
            throw new IllegalArgumentException("Hardware platform cannot be null");
        } else {
            return contentResolver.query(getRawQueryUri(), null, null, getSelectionArgsForRawQuery(hardwarePlatform), null);
        }
    }

    public static void removeAll(ContentResolver contentResolver) {
        int entriesRemoved = contentResolver.delete(TABLE_URI, null, null);
        Trace.debug(TAG, String.format("Removed %d entries", new Object[]{Integer.valueOf(entriesRemoved)}));
    }

    public static String[] getSelectionArgsForRawQuery(HardwarePlatform hardwarePlatform) {
        if (hardwarePlatform == null || hardwarePlatform.getName() == null) {
            return null;
        }
        return new String[]{hardwarePlatform.getName()};
    }

    public static String getRawQuery(String[] selectionArgs) {
        return String.format("SELECT t1._id, t1.file_url, t1.local_language_name, t1.iso_locale, t1.language_version FROM pebble_language_packs t1 WHERE t1.hw_platform = '%s' AND t1.numeric_fw_version = ( SELECT MAX(t2.numeric_fw_version) FROM pebble_language_packs t2 WHERE t2.language_name = t1.language_name AND t2.hw_platform = t1.hw_platform) AND t1.language_version = ( SELECT MAX(t3.language_version) FROM pebble_language_packs t3 WHERE t3.hw_platform = t1.hw_platform AND t3.numeric_fw_version = t1.numeric_fw_version AND t3.language_name = t1.language_name)", new Object[]{selectionArgs[0]});
    }

    public static Uri getRawQueryUri() {
        return CUSTOM_RAW_QUERY_URI;
    }

    public static String getRawQueryTableName() {
        return "language_pack_custom_select_uri";
    }
}
